/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.common.mdb;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import merlion.common.util.Consts;
import merlion.crm.sopm.entity.SalesQuotation;

/**
 *
 * @author Zhang Ying
 */
@MessageDriven(mappedName = "jms/queueSendEmail", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class SendEmailMDBean implements MessageListener {

    @Resource
    private MessageDrivenContext messageDrivenContext;
    @PersistenceContext
    private EntityManager entityManager;

    public SendEmailMDBean() {
    }

    @Override
    public void onMessage(Message message) {
        MapMessage mapMsg = null;
        try {
            if (message instanceof MapMessage) {
                mapMsg = (MapMessage) message;
                String username = mapMsg.getString("username");
                String token = mapMsg.getString("token");
                String email = mapMsg.getString("email");
                String password = mapMsg.getString("password");
                String type = mapMsg.getString("type");

                if (type.equals(Consts.ACCOUNT_ACTIVATION)) {
                    // send account activation email to the staff
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailAccountActivation(username, password, email, token);
                } else if (type.equals(Consts.ACCOUNT_REACTIVATION)) {
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailAccountReactivation(username,email, token, password);
                } else if (type.equals(Consts.ACCOUNT_LOGIN_FAIL)){
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailLoginFail(username,email, token, password);
                } else if (type.equals(Consts.ACCOUNT_RESET)){
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailResetPwd(username,email, token, password);
                } else if (type.equals(Consts.EMAIL_SQ)){
                    // get the object
                    Long sqId = mapMsg.getLong("sqId");
                    byte[] sqContents = mapMsg.getBytes("sqContents");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailSalesQuotation(email, sqId, sqContents);
                }else if(type.equals(Consts.EMAIL_INV)){
                    Long invId = mapMsg.getLong("invId");
                    byte[] invContents = mapMsg.getBytes("invContents");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailInvoice(email, invId, invContents);
                }else if(type.equals(Consts.EMAIL_RMPO)){
                    Long invId = mapMsg.getLong("rmpoId");
                    byte[] invContents = mapMsg.getBytes("rmpoContents");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailRawMaterialPurchaseOrer(email, invId, invContents);
                }else if(type.equals(Consts.EMAIL_REC)){
                    Long invId = mapMsg.getLong("recId");
                    byte[] invContents = mapMsg.getBytes("recContents");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailReceipt(email, invId, invContents);
                }else if(type.equals(Consts.EMAIL_SO)){
                    Long invId = mapMsg.getLong("soId");
                    byte[] invContents = mapMsg.getBytes("soContents");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailSalesOrder(email, invId, invContents);
                }else if (type.equals(Consts.EMAIL_INVOICE_DUE)){
                    String invoiceId = mapMsg.getString("invoiceId");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailInvoiceDueSoon(invoiceId, email, username, password);
                }else if(type.equals(Consts.EMAIL_CUSTOMER_DEFAULT)){
                    String invoiceId = mapMsg.getString("invoiceId");
                    EmailManager emailManager = new EmailManager();
                    emailManager.emailInvoiceDueDefault(invoiceId, email, username);
                }

                System.out.println("Account activation email sending");
            }
        } catch (JMSException jmsEx) {
            jmsEx.printStackTrace();
            messageDrivenContext.setRollbackOnly();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
